class PicoEmitter {
  constructor() {
    try {
      this._t = new EventTarget()
    } catch (e) {
      // EventTarget not supported, use DOM as EventTarget
      this._t = document.createDocumentFragment()
    }
  }
  on(name: string, callback: Function, options?: any) {
    callback._p = (e) => callback.apply(null, e._p)
    this._t.addEventListener(name, callback._p, options)
  }
  off(name: string, callback: Function) {
    this._t.removeEventListener(name, callback._p)
  }
  emit(name: string, ...args: any[]) {
    const e = new Event(name)
    e._p = args
    this._t.dispatchEvent(e)
  }
  once(name: string, callback: Function) {
    this.on(name, callback, { once: true })
  }
}

export default new PicoEmitter()
